package net.jueb.frameworkDemo.orm.mongodb.springDataMongodb.codeStyle;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.math.RandomUtils;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;

import net.jueb.frameworkDemo.orm.entity.Role;
import net.jueb.frameworkDemo.orm.enums.RoleSexEnum;
import net.jueb.frameworkDemo.orm.enums.RoleStatusEnum;
import net.jueb.frameworkDemo.orm.mongodb.springDataMongodb.MyRepository;
import net.jueb.frameworkDemo.orm.mongodb.util.SpringMongoUtil;

public class Test {

	private final MyRepository<Role> repository;
	private final MongoTemplate mongoTemplate;
	public Test() {
		MongoDbFactory mdf=DbUtil.getDbFactory();
		mongoTemplate=SpringMongoUtil.getMongoTemp(mdf,SpringMongoUtil.getDefaultMongoConverter(mdf));
		repository = new DaoRoleImpl(mongoTemplate);
	}
	
	public void init()
	{
		repository.dropCollection();
		repository.createCollection();
	}
	
	public void test1(int count)
	{
		init();
		Role r=null;
		long time=System.nanoTime();
		for(int i=0;i<count;i++)
		{
			Role dc=getTestObject(i);
			repository.saveObject(dc);
			r=dc;
		}
		time=System.nanoTime()-time;
		System.out.println("插入"+count+"条耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒");
		time=System.nanoTime();
		Role result=repository.findObject(r.getId()+"");
		time=System.nanoTime()-time;
		System.out.println("查询"+count+"条 耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒,result:"+result);
	}
	
	public void test2(int count)
	{
		init();
		Role r=null;
		List<Role> dcs=new ArrayList<Role>();
		for(int i=0;i<count;i++)
		{
			Role dc=getTestObject(i);
			r=dc;
			dcs.add(dc);
		}
		long time=System.nanoTime();
		repository.saveObjects(dcs);
		time=System.nanoTime()-time;
		System.out.println("插入"+count+"条耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒");
		time=System.nanoTime();
		Role result=repository.findObject(r.getId()+"");
		time=System.nanoTime()-time;
		System.out.println("查询"+count+"条 耗时:"+time+"纳秒,"+TimeUnit.NANOSECONDS.toMillis(time)+"毫秒,result:"+result);
	}
	
	public Role getTestObject(int index)
	{
		Role role=new Role();
		role.setId(index);
		role.setUid(""+RandomUtils.nextDouble());
		role.setName("index"+RandomUtils.nextDouble());
		role.setPn("test");
		Set<Integer> cannons=new HashSet<Integer>();
		cannons.add(1);
		cannons.add(2);
		role.setCannons(cannons);
		role.setLastLogin(new Date());
		role.setSex(RoleSexEnum.female);
		role.setStatus(RoleStatusEnum.Enable);
		role.setCellPhone("123123123");
		role.setCurrentCannon(1122);
		role.setDiamond(123123);
		role.setLevel(1122);
		role.setMaxBetId(231);
		role.setPwd("123123");
		role.setPhoneMoney(3212312);
		role.setRechargeAmount(3123);
		role.setMoney(34123123);
		role.setPhoneMoney(3123);
		return role;
	}
	
	public void test3() {
		Role role=getTestObject(111);
		mongoTemplate.save(role);
		System.out.println(role);
		Role result=repository.findObject(role.getId()+"");
		System.out.println(result);
	}

	public static void main(String[] args) {
		Test t=new Test();
		t.init();
		t.test3();
		Scanner sc=new Scanner(System.in);
		sc.nextLine();
	}
}
